#!/usr/bin/env python3
"""
对比测试：验证同一个PV在不同时间的数据类型和值
"""

import epics
import time
import numpy as np

def compare_pv_data():
    """对比PV数据获取"""
    
    pv_name = "iLinac_007:BPM6:triggerAmp3_volt"
    print(f"测试PV: {pv_name}")
    
    # 创建PV连接
    pv = epics.PV(pv_name, connection_timeout=5.0)
    
    # 等待连接
    time.sleep(2)
    
    if not pv.connected:
        print(f"错误: 无法连接到 {pv_name}")
        return
    
    print(f"✓ 已连接到 {pv_name}")
    
    # 进行多次数据获取测试
    for i in range(5):
        print(f"\n=== 第 {i+1} 次获取 ===")
        
        # 获取数据
        data = pv.get()
        
        print(f"数据类型: {type(data)}")
        print(f"数据值: {data}")
        
        if data is not None:
            if hasattr(data, '__len__') and not isinstance(data, str):
                print(f"数据长度: {len(data)}")
                if len(data) > 0:
                    if isinstance(data, np.ndarray) or hasattr(data, '__getitem__'):
                        print(f"数据范围: {np.min(data):.6f} ~ {np.max(data):.6f}")
                        print(f"前5个值: {data[:5]}")
            else:
                print("数据为标量值")
        else:
            print("数据为None")
        
        # 等待1秒
        time.sleep(1)

if __name__ == "__main__":
    try:
        compare_pv_data()
    except KeyboardInterrupt:
        print("\n测试被用户中断")
    except Exception as e:
        print(f"测试过程中发生错误: {e}")
        import traceback
        traceback.print_exc()